package com.auoem.radio.ui.other;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.auctus.am.developer.AuHelper;
import com.auctus.am.framework.AmControl;
import com.auctus.am.framework.entity.ChannelInf;
import com.auctus.am.framework.enums.ApiStatusEnum;
import com.auctus.am.framework.gpio.RootCmd;
import com.auctus.am.framework.gpio.RootCmdManager;
import com.auctus.am.framework.interfaces.ApiCallback;
import com.auctus.am.framework.interfaces.IApi;
import com.auctus.am.framework.utils.LogM;
import com.auoem.radio.R;
import com.auoem.radio.base.BaseActivity;
import com.auoem.radio.databinding.ActivittyTestInfBinding;

public class TestSysInfActivity extends BaseActivity implements View.OnClickListener {
    private final static String GPIO_PATH_POWER_EN = "/sys/s910_gpio/dmr_vbat_en";
    private final static String GPIO_PATH_PTT = "/sys/s910_gpio/dmr_tx";

    ActivittyTestInfBinding mBinding;
    private boolean isEnableVbat, isEnableTx;

    IApi mApi;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivittyTestInfBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        mApi = AuHelper.getInstance().getApi();
    }


    @Override
    public void onClick(View v) {
        if (v == mBinding.btChInf) {
            getCurChInf();
        } else if (v == mBinding.btRssi) {
            getRssi();
        } else if (v == mBinding.btChDigGrpId) {
            getGrpId();
        } else if (v == mBinding.btChNum) {
            getChNum();
        } else if (v == mBinding.btChDigIndId) {
            getIndId();
        } else if (v == mBinding.btRadioGrpId) {
            setGrpId();
        } else if (v == mBinding.btRadiIndId) {
            setIndId();
        } else if (v == mBinding.btGrpRev) {
            setGrpRev();
        } else if (v == mBinding.btMicGain) {
            setMicGain();
        } else if (v == mBinding.btcontact) {
            setContact();
        } else if (v == mBinding.btTestCmdTask) {
            testCmdTaskStart();
        } else if (v == mBinding.btOther) {
            testProblem();
        } else if (v == mBinding.btEnableTx) {
            testEnableTx();
        } else if (v == mBinding.btWriteGpio) {
            writeGpio();
        } else if (v == mBinding.btReadGpio) {
            readGpio();
        }


    }

    private void getCurChInf() {
        showProgress(R.string.am_common_exe_ing);
        mApi.getCurChannel(new ApiCallback<ChannelInf>() {
            @Override
            public void onResponse(ApiStatusEnum status, ChannelInf result, String msg) {
                toastResultUI(status);
                if (status == ApiStatusEnum.SUCCESS) {
                    updateStatus(result.toString());
                }
            }
        });
    }

    private void getRssi() {
        showProgress(R.string.am_common_exe_ing);
       mApi.getRssi(new ApiCallback<Integer>() {
            @Override
            public void onResponse(ApiStatusEnum status, Integer result, String msg) {
                toastResultUI(status);
                if (status == ApiStatusEnum.SUCCESS) {
                    updateStatus(result + "");
                }
            }
        });
    }

    private void getChNum() {
        showProgress(R.string.am_common_exe_ing);
       mApi.getFreqBandNum(new ApiCallback<Integer>() {
            @Override
            public void onResponse(ApiStatusEnum status, Integer result, String msg) {
                toastResultUI(status);
                if (status == ApiStatusEnum.SUCCESS) {
                    updateStatus(result + "");
                }
            }
        });
    }

    private void getGrpId() {
        showProgress(R.string.am_common_exe_ing);
       mApi.getGroupId(new ApiCallback<Integer>() {
            @Override
            public void onResponse(ApiStatusEnum status, Integer result, String msg) {
                toastResultUI(status);
                if (status == ApiStatusEnum.SUCCESS) {
                    updateStatus(result + "");
                }
            }
        });
    }

    private void getIndId() {
        showProgress(R.string.am_common_exe_ing);
       mApi.getWlkId(new ApiCallback<Integer>() {
            @Override
            public void onResponse(ApiStatusEnum status, Integer result, String msg) {
                toastResultUI(status);
                if (status == ApiStatusEnum.SUCCESS) {
                    updateStatus(result + "");
                }
            }
        });
    }

    private void setGrpId() {
        String strGrpId = mBinding.etGrpId.getText().toString().trim();
        int iGrpId = 0;
        iGrpId = Integer.parseInt(strGrpId);
       mApi.setGroupId(iGrpId, new ApiCallback() {
            @Override
            public void onResponse(ApiStatusEnum status, Object result, String msg) {
                toastResultUI(status);
            }
        });
    }

    private void setIndId() {
        String strGrpId = mBinding.etIndId.getText().toString().trim();
        int iGrpId = 0;
        iGrpId = Integer.parseInt(strGrpId);
       mApi.setWlkId(iGrpId, true, new ApiCallback() {
            @Override
            public void onResponse(ApiStatusEnum status, Object result, String msg) {
                toastResultUI(status);
            }
        });
    }

    private void setGrpRev() {
        String strGrpId = mBinding.etGrpRev.getText().toString().trim();
        String[] grps = strGrpId.split(",");
       mApi.setGroupCallAry(grps, true, new ApiCallback() {
            @Override
            public void onResponse(ApiStatusEnum status, Object result, String msg) {
                toastResultUI(status);
            }
        });
    }

    private void setMicGain() {
        String strGrpId = mBinding.etMicGain.getText().toString().trim();
        int iGrpId = 0;
        iGrpId = Integer.parseInt(strGrpId);
       mApi.setMicGain(iGrpId, new ApiCallback() {
            @Override
            public void onResponse(ApiStatusEnum status, Object result, String msg) {
                toastResultUI(status);
            }
        });
    }

    private void setContact() {
        String strGrpId = mBinding.etContact.getText().toString().trim();
        int iGrpId = 0;
        iGrpId = Integer.parseInt(strGrpId);
       mApi.setChannelContact(iGrpId, new ApiCallback() {
            @Override
            public void onResponse(ApiStatusEnum status, Object result, String msg) {
                toastResultUI(status);
            }
        });
    }

    private void updateStatus(String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mBinding.tvStatus.setText(msg);
            }
        });
    }

    private void testCmdTaskStart() {
        showProgress(R.string.am_common_exe_ing);
       mApi.moduleConnect(true, new ApiCallback() {
            @Override
            public void onResponse(ApiStatusEnum status, Object result, String msg) {
//                AmHelper.getInstance().setNeedConnect(false);
                toastResultUI(status);
                if (status == ApiStatusEnum.SUCCESS) {
                    cmdGetVer();
                } else {

                }
            }
        });
    }

    private void cmdGetVer() {
       mApi.getRssi(new ApiCallback<Integer>() {
            @Override
            public void onResponse(ApiStatusEnum status, Integer result, String msg) {
                toastResultUI(status);
                if (status == ApiStatusEnum.SUCCESS) {
                    updateStatus(result + "");
                }
            }
        });
    }


    private void testProblem() {
        isEnableVbat = !isEnableVbat;
        RootCmd.writeIo(GPIO_PATH_POWER_EN, isEnableVbat ? new byte[]{0x31, 0x0a} : new byte[]{0x30, 0x0a});
        LogM.d("test write power: " + isEnableVbat);
    }

    private void testEnableTx() {
        isEnableTx = !isEnableTx;
        RootCmd.writeIo(GPIO_PATH_PTT, isEnableTx ? new byte[]{0x31, 0x0a} : new byte[]{0x30, 0x0a});
        LogM.d("test write tx: " + isEnableTx);
    }


    boolean isOn;

    private void writeGpio() {
        String pathVbat = "/sys/s910_gpio/dmr_vbat_en";
        isOn = !isOn;
        RootCmdManager.getInstance().enablePower(isOn);
        mBinding.btWriteGpio.setText(isOn ? "测试写Vat (on)" : "测试写Vat (off)");
    }

    private void readGpio() {
        String pathVbat = "/sys/s910_gpio/dmr_vbat_en";
        RootCmd.readIo(pathVbat);
    }

}
